package com.cgbl.cg.adapter

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.cgbl.cg.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.robot.base.configs.AppConstants
import com.robot.base.model.CommentBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.pic.ImageLoaderUtils

class CourseProfileCommentAdapter(var commentType: Int) :
    BaseQuickAdapter<CommentBean, BaseViewHolder>(R.layout.app_item_course_comment) {

    var onLikeListener: OnLikeListener? = null


    override fun convert(holder: BaseViewHolder, item: CommentBean) {
        ImageLoaderUtils.displayCircle(context, holder.getView(R.id.iv_avatar), item.fromAvatar)
        holder.setText(R.id.tv_nickname, item.fromNickName)
        holder.setText(R.id.tv_comment_content, item.content)
        holder.setText(
            R.id.time,
            TimeUtils.millis2String(item.createTime?.toLong() ?: 0, "yyyy-MM-dd hh:mm:ss")
        )
        holder.setText(
            R.id.tv_comment_zan_num, if (TextUtils.isEmpty(item.likeCount)) "0" else item.likeCount
        )
        holder.setText(
            R.id.tv_comment_num,
            if (item.commentList == null || item.commentList?.isEmpty() == true) "0" else item.commentList!!.size.toString()
        )
        when (item.fromAccountType) {
            "0" -> {
                holder.setText(R.id.item_tv_tag, "官方")
                    .setBackgroundResource(R.id.item_tv_tag, R.drawable.app_radius_2fd39c_6)
                    .setGone(R.id.item_tv_tag, false)
            }

            "1" -> {
                holder.setText(R.id.item_tv_tag, "作者")
                    .setBackgroundResource(R.id.item_tv_tag, R.drawable.app_radius_cc2d2d_6)
                    .setGone(R.id.item_tv_tag, false)
            }

            "2" -> {
                holder.setText(R.id.item_tv_tag, "VIP")
                    .setBackgroundResource(R.id.item_tv_tag, R.drawable.app_radius_ffc063_6)
                    .setGone(R.id.item_tv_tag, false)
            }

            else -> {
                holder.setGone(R.id.item_tv_tag, true)
            }
        }
        holder.setImageResource(
            R.id.item_iv_like, if (item.like == "1") {
                R.mipmap.ic_course_zan_checked
            } else {
                R.mipmap.ic_course_zan_uncheck
            }
        )

        val recyclerViewImg = holder.getView<RecyclerView>(R.id.recyclerView_img)
        val commentImgAdapter = CommentImgAdapter()
        recyclerViewImg.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = commentImgAdapter
        }
        commentImgAdapter.setNewInstance(item.imageFiles as MutableList<String>?)

        val courseProfileCommentReplyAdapter = CourseProfileCommentReplyAdapter()
        val recyclerView = holder.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = courseProfileCommentReplyAdapter
        }

        courseProfileCommentReplyAdapter.addChildClickViewIds(R.id.ll_zan)
        courseProfileCommentReplyAdapter.addChildClickViewIds(R.id.item_layout)
        courseProfileCommentReplyAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.ll_zan -> {
                    onLikeListener?.like(holder.adapterPosition, position)
                }

                else -> {
                    val comment = courseProfileCommentReplyAdapter.getItem(position);
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Mine.A_MINE_COMMENT_INPUT,
                        Bundle().apply {
                            putInt(
                                AppConstants.BundleKey.COMMENT_PRODUCT_TYPE, commentType
                            )
                            putString(AppConstants.BundleKey.COMMENT_PRODUCT_ID, comment.courseId)
                            putString(AppConstants.BundleKey.COMMENT_PARENT_ID, item.id)
                            putString(
                                AppConstants.BundleKey.COMMENT_ACCOUNT_ID, comment.fromAccountId
                            )
                        })
                }
            }

        }

        recyclerView.visibility =
            if (item.commentList?.isEmpty() == true) View.GONE else View.VISIBLE
        courseProfileCommentReplyAdapter.setNewInstance(item.commentList as MutableList<CommentBean>?)
    }

    interface OnLikeListener {
        fun like(itemPosition: Int, childPosition: Int)
    }
}